package com.sikadwa.syc.data

import android.content.Context
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.text.TextUtils
import com.google.gson.Gson
import com.sikadwa.syc.util.checkToString
import com.sikadwa.syc.util.formatZipString
import com.sikadwa.syc.util.getCommonData5
import org.json.JSONObject

/**
 * wifi
 */
class SikadwaSysDataT5(val context: Context) {

    fun getSubmitDataT5(): String? {
        return try {
            val data = hashMapOf<String, Any>().apply {
                put("currentWifi", getSysT5Data1(context))
                put("configuredWifi", "")
                put("wifiCount", "")
            }
            Gson().toJson(data).trim().formatZipString()

        } catch (e: Exception) {
            null
        }
    }

    private fun getSysT5Data1(context: Context): String {
        return try {
            val jsonObject = JSONObject()
            val wifiManager =
                context.getSystemService(Context.WIFI_SERVICE) as WifiManager
            val wifiInfo: WifiInfo? = wifiManager.connectionInfo
            wifiInfo?.let {
                val bssid = wifiInfo.bssid.checkToString()
                val ssid = (wifiInfo.ssid.replace("\"", "")).checkToString()
                val mac = getCommonData5().checkToString()
                val safeBssid = if (TextUtils.isEmpty(bssid)) "" else bssid
                jsonObject.put("bssid", safeBssid)
                jsonObject.put("ssid", ssid)
                jsonObject.put("mac", mac)
            }
            jsonObject.toString()
        } catch (e: Exception) {
            JSONObject().toString()
        }
    }
}